const fp = require("lodash/fp");
class Maybe {
    constructor(value) {
        this._value = value;
    }
    static of(value) {
        return new Maybe(value);
    }
    map(fn) {
        return this.isNothing() ? Maybe.of(null) : Maybe.of(fn(this._value));
    }
    isNothing() {
        return this._value === null || this._value === undefined;
    }
}

let maybe = Maybe.of([5, 6, 1]);

// 2.实现 ex1 函数

const fpAdd = (x) => (y) => x + y;

const fpMap = (fn) => (arr) => arr.map(fn);

const ex11 = fpMap(fpAdd(1));

// 3.调用测试
// console.log(maybe.map(ex11)); // Maybe { _value: [ 6, 7, 2 ] }
// console.log(fp.map(fp.add(1))([6, 7, 2]));
// console.log(
//     maybe.map(ex1).map((x) => {
//         console.log(x);
//         return x;
//     })
// );

class Container {
    constructor(value) {
        this._value = value;
    }
    static of(value) {
        return new Container(value);
    }
    map(fn) {
        return Container.of(fn(this._value));
    }
}

let xs = Container.of(["do", "ray", "me", "fa", "so", "la", "ti", "do"]);

ex2 = fp.first;
ex22 = (arr) => arr[0];

// console.log(xs.map(ex2));

let safeProp = fp.curry(function (x, o) {
    return Maybe.of(o[x]);
});
let user = { id: 2, name: "Albert" };

// 1.实现 ex3
const ex3 = () => safeProp("name", user).map(fp.first);

// 2.测试打印
// console.log(ex3());

const ex4 = (str) => Maybe.of(str).map(parseInt);
console.log(ex4("7R")); // Maybe { _value: 7 }
console.log(ex4("7.6B")); // Maybe { _value: 7 }
console.log(ex4("8.2G")); // Maybe { _value: 8 }

console.log(ex4(null)); // Maybe { _value: null }
console.log(ex4(undefined)); // Maybe { _value: undefined }

console.log(ex4("i7.5")); // Maybe { _value: NaN }
console.log(ex4("abc")); // Maybe { _value: NaN }
